<include file="Public/min-header"/>
<div class="wrapper">
    <include file="Public/breadcrumb"/>
	<section class="content">
       <div class="row">
       		<div class="col-xs-12">
	       		<div class="box">
	             <div class="box-header">
	             	<nav class="navbar navbar-default">	     
				        <div class="collapse navbar-collapse">
				        <form class="navbar-form form-inline" action="" method="post" id="catform">
				        	<div class="form-group">
								<button class="btn bg-navy" type="button" onclick="tree_open(this);"><i class="fa fa-angle-double-down"></i>展开</button>
				        	</div>
				           	<div class="form-group">
				              	<select name="cat_type" class="form-control" style="width:200px;" onchange="$('#catform').submit();">
				              		<option value="">选择分组</option>
				           			<option value="0">默认</option>
				           			<option value="1">系统帮助</option>
				           			<option value="2">系统公告</option>
				              	</select>
				            </div>
				            <div class="form-group pull-right">
					            <a href="{:U('Article/category')}" class="btn btn-primary pull-right"><i class="fa fa-plus"></i>新增分类</a>
				            </div>		          
				          </form>
				      	</div>
	    			</nav> 	               
	             </div><!-- /.box-header -->
	           <div class="box-body">
	           <div class="row">
	            <div class="col-sm-12">
	              <table id="list-table" class="table table-bordered table-striped">
	                 <thead>
	                   <tr role="row">
		                   <th  style="width: 350px;">分类名称</th>
		                   <th>所属分组</th>
		                   <th>描述</th>
		                   <th>是否显示</th>
		                   <th>排序</th>
		                   <th>操作</th>
	                   </tr>
	                 </thead>
					<tbody>
					  <foreach name="cat_list" item="vo" key="k" >
					  	<tr role="row" align="center" class="{$vo.level}" id="{$vo.level}_{$vo.cat_id}">
	                     <td class="sorting_1" align="left" style="padding-left:<?php echo ($vo[level] * 4); ?>em"> 
	                      <if condition="$vo.is_leaf neq 1">
		                      <span class="glyphicon glyphicon-minus btn-warning"  id="icon_{$vo.level}_{$vo.id}" aria-hidden="true" onclick="rowClicked(this)" ></span>&nbsp;				    
					      </if><span>{$vo.name}</span>
					     </td>					     
					     <td>{$type_arr[$vo[cat_type]]}</td>
					     <td>{$vo.cat_desc}</td>
	                     <td>
                             <if condition="$vo[show_in_nav] eq 1"> 显示    
                             <else /> 隐藏</if>
                         </td>
	                 	<td>                         
                            <input type="text"  class="input-sm" onkeyup="this.value=this.value.replace(/[^\d]/g,'')" onpaste="this.value=this.value.replace(/[^\d]/g,'')" onchange="updateSort('article_cat','cat_id','{$vo.cat_id}','sort_order',this)" size="4" value="{$vo.sort_order}" />
                         </td>
	                     <td>
	                      <a class="btn btn-primary" href="{:U('Article/category',array('act'=>'edit','cat_id'=>$vo['cat_id']))}"><i class="fa fa-pencil"></i></a>
	                      <if condition="$vo.cat_type neq 1">
	                      	<a class="btn btn-danger" href="javascript:void(0)" data-url="{:U('Article/categoryHandle')}" data-id="{$vo.cat_id}" onclick="delfun(this)"><i class="fa fa-trash-o"></i></a>
						  <else/>
						  	<a class="btn btn-default disabled" href="javascript:void(0)"><i class="fa fa-trash-o"></i></a>
						  </if>
						</td>
	                   </tr>
	                  </foreach>
	                   </tbody>
	               </table></div></div>
		               <div class="row">
			               <div class="col-sm-5">
			               <div class="dataTables_info" id="example1_info" role="status" aria-live="polite">分页</div></div>                                   
		               </div>
	             </div><!-- /.box-body -->
	           </div><!-- /.box -->
       		</div>
       </div>
     </section>
</div>
<script type="text/javascript">
	function  tree_open(obj)
	{
		var tree = $('#list-table tr[id^="1_"],#list-table tr[id^="2_"], #list-table tr[id^="3_"] '); //,'table-row'
		if(tree.css('display')  == 'table-row')
		{
			$(obj).html("<i class='fa fa-angle-double-down'></i>展开");
			tree.css('display','none');
			$("span[id^='icon_']").removeClass('glyphicon-minus');
			$("span[id^='icon_']").addClass('glyphicon-plus');
		}else
		{
			$(obj).html("<i class='fa fa-angle-double-up'></i>收缩");
			tree.css('display','table-row');
			$("span[id^='icon_']").addClass('glyphicon-minus');
			$("span[id^='icon_']").removeClass('glyphicon-plus');
		}
	}
     
// 以下是 bootstrap 自带的  js
function rowClicked(obj)
{
  span = obj;

  obj = obj.parentNode.parentNode;

  var tbl = document.getElementById("list-table");

  var lvl = parseInt(obj.className);

  var fnd = false;
  
  var sub_display = $(span).hasClass('glyphicon-minus') ? 'none' : '' ? 'block' : 'table-row' ;
  //console.log(sub_display);
  if(sub_display == 'none'){
	  $(span).removeClass('glyphicon-minus btn-info');
	  $(span).addClass('glyphicon-plus btn-warning');
  }else{
	  $(span).removeClass('glyphicon-plus btn-info');
	  $(span).addClass('glyphicon-minus btn-warning');
  }

  for (i = 0; i < tbl.rows.length; i++)
  {
      var row = tbl.rows[i];
      
      if (row == obj)
      {
          fnd = true;         
      }
      else
      {
          if (fnd == true)
          {
              var cur = parseInt(row.className);
              var icon = 'icon_' + row.id;
              if (cur > lvl)
              {
                  row.style.display = sub_display;
                  if (sub_display != 'none')
                  {
                      var iconimg = document.getElementById(icon);
                      $(iconimg).removeClass('glyphicon-plus btn-info');
                      $(iconimg).addClass('glyphicon-minus btn-warning');
                  }else{               	    
                      $(iconimg).removeClass('glyphicon-minus btn-info');
                      $(iconimg).addClass('glyphicon-plus btn-warning');
                  }
              }
              else
              {
                  fnd = false;
                  break;
              }
          }
      }
  }

  for (i = 0; i < obj.cells[0].childNodes.length; i++)
  {
      var imgObj = obj.cells[0].childNodes[i];
      if (imgObj.tagName == "IMG")
      {
          if($(imgObj).hasClass('glyphicon-plus btn-info')){
        	  $(imgObj).removeClass('glyphicon-plus btn-info');
        	  $(imgObj).addClass('glyphicon-minus btn-warning');
          }else{
        	  $(imgObj).removeClass('glyphicon-minus btn-warning');
        	  $(imgObj).addClass('glyphicon-plus btn-info');
          }
      }
  }

}

function delfun(obj){
	if(confirm('确认删除')){		
		$.ajax({
			type : 'post',
			url : $(obj).attr('data-url'),
			data : {act:'del',cat_id:$(obj).attr('data-id')},
			dataType : 'json',
			success : function(data){
				if(data==1){
					$(obj).parent().parent().remove();
				}else{
					layer.alert(data, {icon: 2});  //alert(data);
				}
			}
		})
	}
	return false;
}
</script>
</body>
</html>